/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.lucidhex.viewer.mapimport;

import com.lucidhex.LucidWorld;
import com.lucidhex.hex.LucidHexType;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.MouseInputAdapter;

/**
 *
 * @author Wolf
 */
public class MapImportMouseListener extends MouseInputAdapter{
    
    MapPicture imp;
    Point start = null;
    private final LucidWorld world;
    public MapImportMouseListener(MapPicture imp, LucidWorld world){
        this.imp = imp;
        this.world = world;
    }
    @Override
    public void mousePressed(MouseEvent e){
        if(e.getButton() == MouseEvent.BUTTON1){ 
            start = new Point(e.getX(), e.getY());
        }
    }
    @Override
    public void mouseDragged(MouseEvent e){
         if(start != null){
            Point end = new Point(e.getX(), e.getY());
            imp.setPreCoordinates(start, end);
         }
    }
    @Override
    public void mouseReleased(MouseEvent e){
        Point end = new Point(e.getX(), e.getY());
        if(e.getButton() == MouseEvent.BUTTON1 && start != null){
            imp.setCoordinates(start, end);
        }else if(e.getButton() == MouseEvent.BUTTON3){
            JPopupMenu menu = buildPopMenu(end);
            menu.show(e.getComponent(), e.getX(), e.getY());
        }
        start = null;
    }

    private JPopupMenu buildPopMenu(final Point loc) {
        JPopupMenu jppm = new JPopupMenu();
        for(LucidHexType lht : world.getBoard().getAvailbleHexTypes()){
            jppm.add(buildPopColorItem("Add As "+lht.getTypeName()+" Color", lht, loc));
        }
        return jppm;
    }
    private JMenuItem buildPopColorItem(String id, final LucidHexType aHex, final Point loc){
        JMenuItem out = new JMenuItem(id);
        out.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imp.addColor(loc, aHex);
            }
        });
        return out;
        
    }
}
